﻿
using System;
using Dora.Services.AdminUser.Entities;
using SqlSugar;
using System.Threading.Tasks;
using Dora.Orm.SqlSugarCore.UnitOfWork;
using Surging.Core.Common;
using System.Collections.Generic;

namespace Dora.Services.AdminUser.Repository
{

    /// <summary>
    /// 大社产品类型/目的地保险产品默认排除供应商产品目的地不绑定保险仓储层
    /// </summary>
    public class OrgInsuranceSupplierAreaRepository : SqlSugarRepository<OrgInsuranceSupplierAreaEntity, int>
    {
        private ISugarUnitOfWork _unitOfWork;
        public OrgInsuranceSupplierAreaRepository(ISugarUnitOfWork unitOfWork) : base(unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public async Task<ResultDto<List<OrgInsuranceSupplierAreaEntity>>> BatchInsertOrgInsuranceSupplierAreas(List<OrgInsuranceSupplierAreaEntity> OrgInsuranceExcludes)
        {

            List<OrgInsuranceSupplierAreaEntity> entities = new List<OrgInsuranceSupplierAreaEntity>();

            for (int i = 0; OrgInsuranceExcludes != null && i < OrgInsuranceExcludes.Count; i++)
            {
                OrgInsuranceSupplierAreaEntity OrgInsuranceSupplierEntity = await _unitOfWork.ModifyClient.Queryable<OrgInsuranceSupplierAreaEntity>().
                    Where(t => t.ExcludeSupplierId == OrgInsuranceExcludes[i].ExcludeSupplierId && t.SettingId == OrgInsuranceExcludes[i].SettingId && t.OrgId == OrgInsuranceExcludes[i].OrgId && t.AreaId == OrgInsuranceExcludes[i].AreaId).FirstAsync();
                if (OrgInsuranceSupplierEntity != null)
                {
                    entities.Add(OrgInsuranceSupplierEntity);
                }
                else
                {
                    bool Isok = await _unitOfWork.ModifyClient.Insertable<OrgInsuranceSupplierAreaEntity>(OrgInsuranceExcludes[i]).ExecuteCommandIdentityIntoEntityAsync();
                    entities.Add(OrgInsuranceExcludes[i]);
                }
            }
            return ResultDto<List<OrgInsuranceSupplierAreaEntity>>.Success("成功", entities);
        }
        public async Task<bool> BatchDelOrgInsuranceSupplierAreas(List<OrgInsuranceSupplierAreaEntity> OrgInsuranceExcludes)
        {
            int result = await _unitOfWork.ModifyClient.Deleteable<OrgInsuranceSupplierAreaEntity>(OrgInsuranceExcludes).ExecuteCommandAsync();

            return false;
        }

        public async Task<List<OrgInsuranceSupplierAreaEntity>> GetAllByOrgId(int orgId, int SettingId, int ExcludeSupplierId)
        {
            return await _unitOfWork.QueryClient.
                Queryable<OrgInsuranceSupplierAreaEntity>().
                Where(a => a.OrgId == orgId).
                Where(a => a.SettingId == SettingId).
                Where(a => a.ExcludeSupplierId == ExcludeSupplierId).
                ToListAsync();
        }

        public async Task<List<OrgInsuranceSupplierAreaEntity>> GetAllByOrgId(int orgId, int SettingId, int ExcludeSupplierId,int AreaId)
        {
            return await _unitOfWork.QueryClient.
                Queryable<OrgInsuranceSupplierAreaEntity>().
                Where(a => a.OrgId == orgId).
                Where(a => a.SettingId == SettingId).
                Where(a => a.ExcludeSupplierId == ExcludeSupplierId).
                Where(a => a.AreaId == AreaId).
                ToListAsync();
        }


        public async Task<List<OrgInsuranceSupplierAreaEntity>> GetAllByGetByExcludeSupplierId(int ExcludeSupplierId)
        {
            return await _unitOfWork.QueryClient.
                Queryable<OrgInsuranceSupplierAreaEntity>().
                Where(a => a.ExcludeSupplierId == ExcludeSupplierId).
                ToListAsync();
        }
    }
}